package com.jjsc.product.controller;

import com.jjsc.common.core.web.controller.BaseController;
import com.jjsc.common.core.web.domain.AjaxResult;
import com.jjsc.common.core.web.page.TableDataInfo;
import com.jjsc.common.security.utils.SecurityUtils;
import com.jjsc.product.domain.ProductSpec;
import com.jjsc.product.service.IProductSpecService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: 钟城明
 * @CreateTime: 2025-10-19
 * @Description:
 * @Version: 1.0
 */
@Tag(name = "商品规格")
@RestController
@RequestMapping("/productSpec")
public class ProductSpecController extends BaseController {
    @Autowired
    private IProductSpecService productSpecService;

    /**
     * 查询商品规格列表
     */
    @Operation(summary = "查询商品规格列表")
    @GetMapping("/list")
    public TableDataInfo list(ProductSpec productSpec) {
        startPage();
        List<ProductSpec> list = productSpecService.selectProductSpecList(productSpec);
        return getDataTable(list);
    }

    /**
     * 根据id查询商品规格详细信息
     */
    @Operation(summary = "根据id查询商品规格详细信息")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return AjaxResult.success(productSpecService.selectProductSpecById(id));
    }

    /**
     * 保存商品规格
     */
    @Operation(summary = "保存商品规格")
    @PostMapping
    public AjaxResult add(@RequestBody ProductSpec productSpec) {
        productSpec.setCreateBy(SecurityUtils.getUsername());
        return toAjax(productSpecService.save(productSpec));
    }

    /**
     * 修改商品规格
     */
    @Operation(summary = "修改商品规格")
    @PutMapping
    public AjaxResult edit(@RequestBody ProductSpec productSpec) {
        productSpec.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(productSpecService.updateById(productSpec));
    }

    /**
     * 根据id批量删除商品规格
     */
    @Operation(summary = "根据id批量删除商品规格")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(productSpecService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 根据分类id查询商品规格列表
     */
    @Operation(summary = "根据分类id查询商品规格列表")
    @GetMapping("/productSpec/{categoryId}")
    public AjaxResult listByCategoryId(@PathVariable Long categoryId) {
        List<ProductSpec> list = productSpecService.selectProductSpecListByCategoryId(categoryId);
        return AjaxResult.success(list);
    }
}
